package cn.edu.buaa.purain.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public abstract class PurainDBAdapter{

	public Object executeQuery(String sql, Object[] objs, String dbName)
			throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Object rtObj = null;
		
		try {
			conn = PurainDBManager.getConnection(dbName);
			ps = PurainDBManager.getPreparedStatement(conn, sql);
			
			if (null != objs) {
				for (int i = 1; i <= objs.length; i++) {
					ps.setObject(i, objs[i-1]);
				}
			}
			rs = PurainDBManager.getResultSet(ps);
		
			rtObj = processResultSet(rs);
		}catch(Exception ex) {
			throw ex;
		}finally {
			PurainDBManager.closeResultSet(rs);
			PurainDBManager.closePreparedStatement(ps);
			PurainDBManager.closeConnection(conn);
		}
		
		return rtObj;
	}

	protected abstract Object processResultSet(ResultSet rs) throws Exception;

}
